home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / Warrior’s Progress / source code / Source / Libraries / Memory / Stacks / FiniteStackBase.cp < prev    next >
Encoding:
Text File  |  1997-06-28  |  1.1 KB  |  48 lines  |  [TEXT/CWIE]

  1. // FiniteStackBase.cp
  2.  
  3. #ifndef FiniteStackBase_h
  4. #include "FiniteStackBase.h"
  5. #endif
  6. #ifndef Assert_h
  7. #include "Assert.h"
  8. #endif
  9.  
  10. FiniteStackBase::FiniteStackBase( void *theStart,
  11.                                              uint32 theLength,
  12.                                              uint32 theElementSize )
  13.   : start( static_cast<uint8 *>(theStart) ),
  14.      end( static_cast<uint8 *>(theStart) + theLength * theElementSize ),
  15.      elementSize( theElementSize ),
  16.      allocateNext( static_cast<uint8 *>(theStart) )
  17.   {
  18.     Assert( CanMultiply( theLength, theElementSize ) )
  19.   }
  20.  
  21. void *FiniteStackBase::Allocate( uint32 size )
  22.   {
  23.     Assert( size == elementSize );
  24.     Assert( !Full() );
  25.     
  26.     Assert( allocateNext >= start );
  27.     Assert( ( allocateNext - start ) % elementSize == 0 );
  28.  
  29.     void *result = allocateNext;
  30.     
  31.     allocateNext += elementSize;
  32.     Assert( allocateNext <= end );
  33.     
  34.     return result;
  35.   }
  36.  
  37. void FiniteStackBase::Release( void *p )
  38.   {
  39.     Assert( !IsEmpty() );
  40.     Assert( p == allocateNext - elementSize );
  41.  
  42.     Assert( allocateNext <= end );
  43.     Assert( ( allocateNext - start ) % elementSize == 0 );
  44.     
  45.     allocateNext -= elementSize;
  46.     Assert( allocateNext >= start );
  47.   }
  48.